package com.yhsimon.singleton.type7;

public class SingletonTest7 {
    public static void main(String[] args) {
        System.out.println("使用静态内部类实现单例模式");
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println("i1与i2实例是否相等：" + (instance1 == instance2));
        System.out.println("i1与i2的哈希值是否相同：i1的哈希值为 " + instance1.hashCode() + "  i2的哈希值为：" + instance2.hashCode() + "---" + (instance1.hashCode() == instance2.hashCode()));
    }

}

//静态内部类，推荐使用
class Singleton {
    private static volatile Singleton instance;

    private Singleton() {
    }

    //写一个静态内部类，该类有一个静态属性Singleton
    private static class SingletonInstance{
        private static final Singleton INSTANCE=new Singleton();
    }

    //提供一个静态的公有方法，直接返回SingletonInstance.INSTANCE
    public static synchronized Singleton getInstance(){
        return SingletonInstance.INSTANCE;
    }


}